package com.example.teamtribe.database;

import android.os.Parcel;
import android.os.Parcelable;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class User extends DataSupport implements Parcelable {

    private int id;
    private int realId;
    private String nickname;
    private String portrait;//头像存储在服务器的文件名
    private int isPublic;

    private int schoolId;
    private String no;
    private String name;
    private int gender;
    private String birthday;
    private String academy;
    private String major;
    private int grade;
    private int cla;

    private List<Integer> friends;

    public User() {
    }

    public User(int realId) {
        this.realId = realId;
    }

    public String getGenderString() {
        if (getIsPublicArray()[5] == '0') {
            return "";
        }
        if (gender == 1) {
            return "男";
        }
        return "女";
    }

    public char[] getIsPublicArray() {
        char[] temp = "000000000".toCharArray();
        char[] chars = Integer.toBinaryString(isPublic).toCharArray();
        for (int i = 8, j = chars.length - 1; j >= 0; i--, j--) {
            temp[i] = chars[j];
        }
        return temp;
    }

    public void setIsPublicArray(char[] isPublicArray) {
        isPublic = Integer.parseInt(new String(isPublicArray), 2);
    }

    public synchronized boolean saveOrUpdate() {
        return saveOrUpdate("realId = ?", realId + "");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof User) {
            User user = (User) obj;
            return this.realId == user.realId;
        }
        return false;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getRealId() {
        return realId;
    }

    public void setRealId(int realId) {
        this.realId = realId;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public int getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(int isPublic) {
        this.isPublic = isPublic;
    }

    public int getSchoolId() {
        if (schoolId == 0)
            return 1;
        return schoolId;
    }

    public void setSchoolId(int schoolId) {
        this.schoolId = schoolId;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getAcademy() {
        return academy;
    }

    public void setAcademy(String academy) {
        this.academy = academy;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getCla() {
        return cla;
    }

    public void setCla(int cla) {
        this.cla = cla;
    }

    public List<Integer> getFriends() {
        return friends;
    }

    public void setFriends(List<Integer> friends) {
        this.friends = friends;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeInt(this.realId);
        dest.writeString(this.nickname);
        dest.writeString(this.portrait);
        dest.writeInt(this.isPublic);
        dest.writeInt(this.schoolId);
        dest.writeString(this.no);
        dest.writeString(this.name);
        dest.writeInt(this.gender);
        dest.writeString(this.birthday);
        dest.writeString(this.academy);
        dest.writeString(this.major);
        dest.writeInt(this.grade);
        dest.writeInt(this.cla);
        dest.writeList(this.friends);
    }

    protected User(Parcel in) {
        this.id = in.readInt();
        this.realId = in.readInt();
        this.nickname = in.readString();
        this.portrait = in.readString();
        this.isPublic = in.readInt();
        this.schoolId = in.readInt();
        this.no = in.readString();
        this.name = in.readString();
        this.gender = in.readInt();
        this.birthday = in.readString();
        this.academy = in.readString();
        this.major = in.readString();
        this.grade = in.readInt();
        this.cla = in.readInt();
        this.friends = new ArrayList<Integer>();
        in.readList(this.friends, Integer.class.getClassLoader());
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", realId=" + realId +
                ", nickname='" + nickname + '\'' +
                ", portrait='" + portrait + '\'' +
                ", isPublic=" + isPublic +
                ", schoolId=" + schoolId +
                ", no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", gender=" + gender +
                ", birthday='" + birthday + '\'' +
                ", academy='" + academy + '\'' +
                ", major='" + major + '\'' +
                ", grade=" + grade +
                ", cla=" + cla +
                ", friends=" + friends +
                '}';
    }
}
